<?php 


class CommonModules{
	
	public static $msgs = array();
	var $module = '';
		
	function __construct($module)
	{
		$this->module = $module;
	}
	
	function save($data)
	{
		$_action = $this->module;
		switch($_action)
		{
			case 'questionByUser':
				return $this->save_questionByUser($data);
			break;
			
			case 'issueByUser':
				return $this->save_issueByUser($data);
			break;
			
			case 'ideaByUser':
				return $this->save_ideaByUser($data);
			break;
		}		
	}
	
	function save_ideaByUser($data)
	{
		global $db;
		$record = array(
							"user_id" => $data['user_id'],
							"idea_stmt" => $data['idea_stmt'],
							"idea_description" => $data['idea_description'],
							"created_on" => date('Y-m-d H:i:s'),
						);
		if( $db->insert("user_ideas", $record) ){
			echo 1;	
		} else {
			echo 0;	
		}
	}
	
	function save_questionByUser($data)
	{
		global $db;
		$record = array(
							"user_id" => $data['user_id'],
							"question_stmt" => $data['question_stmt'],
							"question_description" => $data['question_description'],
							"created_on" => date('Y-m-d H:i:s')
						);
		if( $db->insert("user_questions", $record) ){
			echo 1;	
		} else {
			echo 0;	
		}
	}
	
	function save_issueByUser($data)
	{
		global $db;
		$record = array(
							"user_id" => $data['user_id'],
							"issue_stmt" => $data['issue_stmt'],
							"issue_description" => $data['issue_description'],
							"created_on" => date('Y-m-d H:i:s')
						);
		if( $db->insert("user_issues", $record) ){
			echo 1;	
		} else {
			echo 0;	
		}
	}
	
	function getList($data)
	{
		$_action = $this->module;
		switch($_action)
		{
			case 'questionByUser':
				return $this->getList_questionByUser($data);
			break;
			
			case 'issueByUser':
				return $this->getList_issueByUser($data);
			break;
			
			case 'ideaByUser':
				return $this->getList_ideaByUser($data);
			break;
		}		
	}

	function getList_ideaByUser($data)
	{
		global $db, $pager, $config;
		$pager = new Paginator();
		$keyword = isset($data['keyword']) ? trim($data['keyword']) : '';
		
		$where = '';
		if( $keyword != '')	{
			$where = " WHERE question_stmt like '%" . $db->escape($keyword) . "%'" ;
		}

		$config['page_limit'] =	( intval($data['new_page_limit']) > 0 ) ? $data['new_page_limit'] : $config['page_limit'];
		$page_limit = $config['page_limit']; 
		if(isset($_GET['pg'])&& $_GET['pg'] > 0 ){ 
			$limit_lenght  = (($_GET['pg']-1)*$page_limit) ; 
			$limit = "limit $limit_lenght, $page_limit ";
		}else{
			$limit = "limit 0 , $page_limit ";
			if($data['new_page_limit'] == 'all') 
				$limit = '';
		}

		$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM user_ideas order by id desc $limit "; 
		$result = $db->fetch_all($sql);
 		$counter = $db->queryCount();
		$pager->items_total = $counter[0];
		$pager->default_ipp = ($data['new_page_limit'] == 'all') ? $pager->items_total : $config['page_limit'];
		$pager->paginate($path);
		return $result;
	
	}

	function getList_issueByUser($data)
	{
		global $db, $pager, $config;
		$pager = new Paginator();
		$keyword = isset($data['keyword']) ? trim($data['keyword']) : '';
		
		$where = '';
		if( $keyword != '')	{
			$where = " WHERE question_stmt like '%" . $db->escape($keyword) . "%'" ;
		}

		$config['page_limit'] =	( intval($data['new_page_limit']) > 0 ) ? $data['new_page_limit'] : $config['page_limit'];
		$page_limit = $config['page_limit']; 
		if(isset($_GET['pg'])&& $_GET['pg'] > 0 ){ 
			$limit_lenght  = (($_GET['pg']-1)*$page_limit) ; 
			$limit = "limit $limit_lenght, $page_limit ";
		}else{
			$limit = "limit 0 , $page_limit ";
			if($data['new_page_limit'] == 'all') 
				$limit = '';
		}

		$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM user_issues order by id desc $limit "; 
		$result = $db->fetch_all($sql);
 		$counter = $db->queryCount();
		$pager->items_total = $counter[0];
		$pager->default_ipp = ($data['new_page_limit'] == 'all') ? $pager->items_total : $config['page_limit'];
		$pager->paginate($path);
		return $result;
	
	}


	function getList_questionByUser($data)
	{
		global $db, $pager, $config;
		$pager = new Paginator();
		$keyword = isset($data['keyword']) ? trim($data['keyword']) : '';
		
		$where = '';
		if( $keyword != '')	{
			$where = " WHERE question_stmt like '%" . $db->escape($keyword) . "%'" ;
		}

		$config['page_limit'] =	( intval($data['new_page_limit']) > 0 ) ? $data['new_page_limit'] : $config['page_limit'];
		$page_limit = $config['page_limit']; 
		if(isset($_GET['pg'])&& $_GET['pg'] > 0 ){ 
			$limit_lenght  = (($_GET['pg']-1)*$page_limit) ; 
			$limit = "limit $limit_lenght, $page_limit ";
		}else{
			$limit = "limit 0 , $page_limit ";
			if($data['new_page_limit'] == 'all') 
				$limit = '';
		}

		$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM user_questions order by id desc $limit "; 
		$result = $db->fetch_all($sql);
 		$counter = $db->queryCount();
		$pager->items_total = $counter[0];
		$pager->default_ipp = ($data['new_page_limit'] == 'all') ? $pager->items_total : $config['page_limit'];
		$pager->paginate($path);
		return $result;
	
	}
	
	function delete($data)
	{
		$_action = $this->module;
		switch($_action)
		{
			case 'questionByUser':
				$this->delete_questionByUser($data);
			break;
			
			case 'issueByUser':
				$this->delete_issueByUser($data);
			break;
			
			case 'ideaByUser':
				$this->delete_ideaByUser($data);
			break;
		}		
	}
	
	function delete_ideaByUser($data)
	{
		global $db;
		$where = array( 'id' => $data['id'] );
		if( $db->delete("user_ideas", $where) )
		{ 
			redirect("admin/user_ideas.php?deleted=1");	
		}
	}
	
	function delete_issueByUser($data)
	{
		global $db;
		$where = array( 'id' => $data['id'] );
		if( $db->delete("user_issues", $where) )
		{ 
			redirect("admin/user_issues.php?deleted=1");	
		}
	}
	
	function delete_questionByUser($data)
	{
		global $db;
		$where = array( 'id' => $data['id'] );
		if( $db->delete("user_questions", $where) )
		{ 
			redirect("admin/user_questions.php?deleted=1");	
		}
	}
	
}


?>